/*
 * author:tim
 * date: 9.21
 */
#include "ui_time.h"

class control_student: public QWidget
{
    private:
        Ui::student* my_ui;
        student* students;
        int num;
        void (*quick_sort)(student*, student*);
    public:
        void summary()
        {
            std::ifstream infos;
            infos.open(my_ui->lineEdit->text().toStdString());
            std::string info_;
            int cnt = 0;
            double all_avg;

            while (getline(infos, info_)) {
                std::istringstream info(info_);
                students[cnt].set_stu_inf(info);
                all_avg += students[cnt++].Average();
            }
            all_avg /= 3;

            quick_sort(students, students + 2);
            std::cout<<"yes\n";
            my_ui->textBrowser->setText(QString::fromStdString(std::to_string(students[2].Average())));
            my_ui->textBrowser_2->setText(QString::fromStdString(std::to_string(all_avg)));
        }
        void query()
        {
            std::string name(my_ui->lineEdit_2->text().toStdString());
            std::ostringstream oinfo;
            for (int i = 0; i < num; i++)
                students[i].get_by_id(name, oinfo);
            my_ui->textBrowser_3->setText(QString::fromStdString(oinfo.str()));
        }
        control_student(Ui::student* _ui, student* _s, int _n, void (*f)(student*, student*) ):
            my_ui(_ui), students(_s), num(_n), quick_sort(f)
        {
            my_ui->setupUi(my_ui);
            QObject::connect(my_ui->pushButton, &QPushButton::clicked, this, &control_student::summary);
            QObject::connect(my_ui->pushButton_2, &QPushButton::clicked, this, &control_student::query);
            QMetaObject::connectSlotsByName(this);
        }
};